#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import yaml
import MySQLdb
from sklearn.feature_extraction import DictVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
import csv
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt
import time
import datetime
import scipy as sp


def dtime(dt):
    #12/07/2014-22:44:37.169789
    #myDate = "12/07/2014-22:44:37.169789"
    #myDate = "2014-08-01 04:41:52,117"
    timestamp = time.mktime(datetime.datetime.strptime(dt, "%d/%m/%Y-%H:%M:%S.%f").timetuple())
    return dt

#EFETUA LIGACAO
def db_establish_conn(yaml_options):
    try:
        database = MySQLdb.connect(host=yaml_options['Database']['host'],user=yaml_options['Database']['user'],passwd=yaml_options['Database']['pass'],db=yaml_options['Database']['dbname'] )
        cur = database.cursor()
        return database, cur

    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0],e.args[1])
        sys.exit(1)


#LISTA BD
def list_it(yaml):
    #(db, cursor) = db_establish_conn(yaml)

    #cursor.execute("SELECT * FROM filejson")

    #rows = cursor.fetchall()
    ## vai buscar à bd e mete no csv
    #with open("out.csv", "wb") as csv_file:
    #    csv_writer = csv.writer(csv_file)
    #    csv_writer.writerow([i[0] for i in cursor.description]) #write headers
    #    csv_writer.writerows(cursor)  

    #abre o csv preenchido
    f = open("out-.csv")
    f.readline() #remove headers
    lines = f.read().split("\n") # "\r\n" if needed
    cols = []
    
    a = []
    i=0

    b=''

    csvReader = csv.reader(open('out-.csv', 'rb'), delimiter=' ', quotechar='|')
    
    data = sp.genfromtxt("out-.csv", delimiter=",")

    for row in csvReader:
        a.append(row)

    #x = a[:,0]
    #print data
    #my_list 
    t = []
    for i in range(0, len(a)):
        #print a[i]
        my_list = a[0:].split(',')
        t.append(my_list[0])

    print t
    #    print a[i]
    #for line in lines:
    #if line != "": # add other needed checks to skip titles
        #cols = line.split(",")
        #print cols

    #for line in f:
    #    line = line.strip().split(",")
    #    #print line
    #    data.append([str(x) for x in line])

    #train = data
    #print data
    #x = data[:,0]
    #y = data[:,6]

    #print x
    print "////////////////////////////////////////////////////////////////////////////////////////"

    print cols
    data2 = []
    #with open('out.csv') as csvfile:
        #reader = csv.DictReader(csvfile)
        #for row in reader:
            #data2 = data2.append([row['time_received'], row['md5']])
            #print(row['time_received'], row['md5'])
    

    #    vectorizer = CountVectorizer(min_df=1)
    
#    x = vectorizer.fit_transform(train[0])
#    Y = vectorizer.fit_transform(train[0])
    print "////////////////////////////////////////////////////////////////////////////////////////"
    
#    XX = x.toarray()
#    X = np.transpose(XX)
    #print X
    #print X.toarray()[0]#Talvez estaja no caminho certo
#ver pag http://scikit-learn.org/stable/modules/feature_extraction.html
#no ponto 4.1.3.3. Common Vectorizer usage

#    target = [x[0] for x in train]
#    train = [x[1:] for x in train]

#    rf = RandomForestClassifier(n_estimators=100)
#    rf.fit(train, target)
#    predicted_probs = rf.predict_proba(test)
#    predicted_probs = [["%f" % x[1]] for x in predicted_probs]

#    s = open("saida.csv","w")
#    for line in predicted_probs: f.write(",".join(line) + "\n")
    #np.sin(X).ravel()
    
#    y = XX[0]
#    print XX
#    print "////////////////////////////////////////////////////////////////////////////////////////"
#    print XX[0]
#    print "////////////////////////////////////////////////////////////////////////////////////////"
    ###############################################################################
    # Add noise to targets
    #y[::5] += 3 * (0.5 - np.random.rand(8))

    ###############################################################################
    # Fit regression model
#    svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
#    svr_lin = SVR(kernel='linear', C=1e3)
#    svr_poly = SVR(kernel='poly', C=1e3, degree=2)
#    y_rbf = svr_rbf.fit(X, y).predict(X)
#    y_lin = svr_lin.fit(X, y).predict(X)
#    y_poly = svr_poly.fit(X, y).predict(X)
   
#    ###############################################################################
#    # look at the results
#    #plt.scatter(X, y, c='k', label='data')
#    plt.hold('on')
#    plt.plot(X, y_rbf, c='g', label='RBF model')
#    plt.plot(X, y_lin, c='r', label='Linear model')
#    plt.plot(X, y_poly, c='b', label='Polynomial model')
#    plt.xlabel('data')
#    plt.ylabel('target')
#    plt.title('Support Vector Regression')
#    #plt.legend()
#    plt.show()


#PARSE DO FICHEIRO YAML
class ParseYamlConfig():
    def parse(self):
	try:
            f = open('db.yaml', 'r')
	except IOError as e:
	    print "I/O error({0}): {1}".format(e.errno, e.strerror), "ERROR !!!"
		
	self.dataMap = yaml.load(f)
	f.close()
        
        return self.dataMap;


def main():    
    yaml_options = ParseYamlConfig().parse()

    list_it(yaml_options)

 
if __name__ == "__main__":

    main()
